<?php
namespace jm\wechat;
use jm\wechat\common\WeChatCommon;
use think\Db;
use think\Config;
/**
 * 提现管理
 * jm\WeChat\WeChatWithdraw
 * @icon fa fa-circle-o
 */
class WeChatWithdraw 
{
        public $appid = 'wxbf9588a6bd5e6d59';
     	public $mch_id = '1602988750';
     	public $key = '2e8baf653d4647649c07169ca11b0527';
     	public $apiclient_cert = './wxpay/apiclient_cert.pem';
     	public $apiclient_key = './wxpay/apiclient_key.pem'; 
          //私有化构造函数，防止外界实例化对象
      public function __construct($data)
      {
        $this->appid=isset($data["appid"])&&!empty($data["appid"])?$data["appid"]:$this->appid;
        $this->mch_id=isset($data["mch_id"])&&!empty($data["mch_id"])?$data["mch_id"]:$this->mch_id;
        $this->key=isset($data["key"])&&!empty($data["key"])?$data["key"]:$this->key;
        $this->apiclient_cert=isset($data["apiclient_cert"])&&!empty($data["apiclient_cert"])?$data["apiclient_cert"]:$this->apiclient_cert;
        $this->apiclient_key=isset($data["apiclient_key"])&&!empty($data["apiclient_key"])?$data["apiclient_key"]:$this->apiclient_key;
      }
     
     
    public function WechatWithdrawal($openId,$money)   {

        // $appid = Config::get("site.Applets_appid");//商户账号appid
        // $appid = Config::get("site.wechat_pay_appid");//商户账号appid
        $appid = $this->appid;
        $mch_id = $this->mch_id; //商户号 
        $key = $this->key; 
        $openid = $openId;//授权用户openid
        $common=new  WeChatCommon($key);//调用公共方法
    //   var_dump($openId,$appid,$mch_id,$key);
    //   die;
        $arr = array();
        $arr['partner_trade_no'] = '123456789' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商户订单号
        $arr['openid'] = $openid;
        $arr['amount'] = $money * 100;//付款金额，单位为分
        
        $arr['mch_appid'] = $appid;
        $arr['mchid'] = $mch_id;
        $arr['nonce_str'] = $common->getNonceStr();//公共方法的随机字符串
        $arr['desc'] = "零钱提现";//描述信息
        $arr['check_name'] = 'NO_CHECK';//是否验证用户真实姓名，这里不验证
        // $arr['spbill_create_ip'] = 'xx.xx.xx.xx';//获取服务器的ip
        //封装的关于签名的算法
        $arr['sign'] = $common->makeSign($arr,$key);//签名
        $var = $common->array2xml($arr);
        // dump($arr['sign'] );exit;
        $xml = $this->curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers',$var,30, array(), 1);
        libxml_disable_entity_loader(true);
		//echo $xml; die;
		$obj1=simplexml_load_string($xml,'SimpleXMLElement');
//var_dump($obj1); die;
        $rdata = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
        // var_dump('cash_xmldata',$rdata);//eblog('cash_xmldata',$rdata);
 
      
        $return_code = trim(strtoupper($rdata['return_code']));
        $result_code = trim(strtoupper($rdata['result_code']));
        if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') {
            $isrr = array(
                'status'=>1,
                'msg' => '成功',
            );
        } else {
            
            // $returnmsg = $rdata['return_msg'];
            $err_code_des = $rdata['err_code_des'];
            $isrr = array(
                'status' => 0,
                'msg' => $err_code_des,
            );
        }
        return $isrr;
    } 
    protected function curl_post_ssl($url, $vars, $second = 30, $aHeader = array())
    {
 
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
        curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
        // curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
        // curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
        // curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
        
        curl_setopt($ch,CURLOPT_SSLCERT, $this->apiclient_cert); 
        curl_setopt($ch,CURLOPT_SSLKEY, $this->apiclient_key);
        
        // curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
        // curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
        if (count($aHeader) >= 1) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
        }
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//全部数据使用HTTP协议中的"POST"操作来发送
        $data = curl_exec($ch);//执行回话
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    } 
    
}